<script>
    import CommonHeader from './CommonHeader.vue' // 导入公共页头组件
    export default{
        components: {
            CommonHeader
        },
        data() {
            return {
                userList: [
                    {id: 1, name: '张三', age: 18},
                    {id: 3, name: '李四', age: 23},
                    {id: 4, name: '李夏青', age: 19},
                    {id: 5, name: '王五', age: 21},
                    {id: 6, name: '赵六', age: 30},
                    {id: 7, name: '谢静', age: 16},
                ],
                sortField: 'id',
                sortDirection: 'asc'
            }
        },
        methods: {
            sortTable(field) {
                if (this.sortField === field) {
                    this.sortDirection = this.sortDirection === 'asc' ? 'desc' : 'asc';
                } else {
                    this.sortField = field;
                    this.sortDirection = 'asc';
                }
                
                this.userList.sort((a, b) => {
                    if (this.sortDirection === 'asc') {
                        return a[field] > b[field] ? 1 : -1;
                    } else {
                        return a[field] < b[field] ? 1 : -1;
                    }
                });
            }
        }
    }
</script>

<template>
    <div class="user-management-container">
        <CommonHeader title="用户列表"></CommonHeader>
        
        <div class="user-list-wrapper">
            <div class="user-list-header">
                <h2 class="user-list-title">系统用户管理</h2>
                <div class="user-list-actions">
                    <div class="search-box">
                        <input type="text" placeholder="搜索用户..." v-model="searchQuery">
                        <i class="fa fa-search"></i>
                    </div>
                    <button class="btn-add">
                        <i class="fa fa-plus mr-1"></i>新增用户
                    </button>
                </div>
            </div>
            
            <div class="table-responsive">
                <table class="user-table">
                    <thead>
                        <tr>
                            <th @click="sortTable('id')">
                                <div class="th-content">
                                    编号
                                    <i class="fa fa-sort ml-1"></i>
                                </div>
                            </th>
                            <th @click="sortTable('name')">
                                <div class="th-content">
                                    姓名
                                    <i class="fa fa-sort ml-1"></i>
                                </div>
                            </th>
                            <th @click="sortTable('age')">
                                <div class="th-content">
                                    年龄
                                    <i class="fa fa-sort ml-1"></i>
                                </div>
                            </th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in userList" :key="item.id" :class="{'bg-gray-50': index % 2 === 0}">
                            <td class="text-center">{{ item.id }}</td>
                            <td>
                                <div class="user-info">
                                    <div class="user-avatar">
                                        <img :src="'https://picsum.photos/id/' + (item.id * 10) + '/40/40'" alt="用户头像">
                                    </div>
                                    <span>{{ item.name }}</span>
                                </div>
                            </td>
                            <td class="text-center">
                                <span class="age-badge" :class="{'badge-youth': item.age < 20, 'badge-adult': item.age >= 20 && item.age < 30, 'badge-senior': item.age >= 30}">
                                    {{ item.age }}
                                </span>
                            </td>
                            <td class="actions-cell">
                                <button class="btn-view">
                                    <i class="fa fa-eye"></i>
                                </button>
                                <button class="btn-edit">
                                    <i class="fa fa-pencil"></i>
                                </button>
                                <button class="btn-delete">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <div class="pagination-container">
                <div class="pagination-info">
                    共 {{ userList.length }} 条记录
                </div>
                <div class="pagination-controls">
                    <button class="page-btn disabled">
                        <i class="fa fa-angle-left"></i>
                    </button>
                    <button class="page-btn active">1</button>
                    <button class="page-btn">2</button>
                    <button class="page-btn">3</button>
                    <button class="page-btn">
                        <i class="fa fa-angle-right"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
/* 基础布局 */
.user-management-container {
    padding: 1rem 1.5rem;
    max-width: 1920px;
    margin: 0 auto;
}

.user-list-wrapper {
    background-color: #fff;
    border-radius: 0.5rem;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    padding: 1.5rem;
    margin-top: 1rem;
}

.user-list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.user-list-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #334155;
}

.user-list-actions {
    display: flex;
    gap: 1rem;
}

.search-box {
    position: relative;
    display: flex;
    align-items: center;
    background-color: #f8fafc;
    border-radius: 0.375rem;
    padding: 0.5rem 0.75rem;
    width: 240px;
    border: 1px solid #e2e8f0;
    transition: border-color 0.2s;
}

.search-box:hover,
.search-box:focus-within {
    border-color: #94a3b8;
}

.search-box input {
    background: transparent;
    border: none;
    width: 100%;
    outline: none;
    font-size: 0.9rem;
    color: #334155;
}

.search-box input::placeholder {
    color: #94a3b8;
}

.search-box i {
    margin-left: 0.5rem;
    color: #94a3b8;
}

.btn-add {
    background-color: #165DFF;
    color: white;
    border: none;
    border-radius: 0.375rem;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background-color 0.2s;
    display: flex;
    align-items: center;
}

.btn-add:hover {
    background-color: #0d47a1;
}

.table-responsive {
    overflow-x: auto;
}

.user-table {
    width: 100%;
    border-collapse: collapse;
}

.user-table th,
.user-table td {
    padding: 0.75rem 1rem;
    text-align: left;
}

.user-table th {
    background-color: #f8fafc;
    font-weight: 600;
    color: #475569;
    border-bottom: 2px solid #e2e8f0;
}

.th-content {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.th-content:hover {
    color: #165DFF;
}

.user-table td {
    border-bottom: 1px solid #e2e8f0;
    color: #334155;
}

.user-table tr:last-child td {
    border-bottom: none;
}

.user-table tr:hover {
    background-color: #f1f5f9;
    transition: background-color 0.2s;
}

.user-info {
    display: flex;
    align-items: center;
}

.user-avatar {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 0.75rem;
}

.user-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.age-badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.8rem;
    font-weight: 500;
}

.badge-youth {
    background-color: #e0f2fe;
    color: #0369a1;
}

.badge-adult {
    background-color: #dbeafe;
    color: #1d4ed8;
}

.badge-senior {
    background-color: #e0e7ff;
    color: #4f46e5;
}

.actions-cell {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
}

.btn-view,
.btn-edit,
.btn-delete {
    width: 2rem;
    height: 2rem;
    border-radius: 0.375rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s;
    border: none;
}

.btn-view {
    background-color: #f0f9ff;
    color: #0ea5e9;
}

.btn-view:hover {
    background-color: #e0f2fe;
}

.btn-edit {
    background-color: #fef6e4;
    color: #f59e0b;
}

.btn-edit:hover {
    background-color: #fdf2e0;
}

.btn-delete {
    background-color: #fee2e2;
    color: #ef4444;
}

.btn-delete:hover {
    background-color: #fdd7d7;
}

.pagination-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid #e2e8f0;
}

.pagination-info {
    color: #64748b;
    font-size: 0.9rem;
}

.pagination-controls {
    display: flex;
    gap: 0.25rem;
}

.page-btn {
    width: 2rem;
    height: 2rem;
    border-radius: 0.375rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s;
    border: 1px solid #e2e8f0;
    color: #64748b;
    background-color: #fff;
}

.page-btn:hover:not(.disabled):not(.active) {
    background-color: #f8fafc;
}

.page-btn.active {
    background-color: #165DFF;
    color: white;
    border-color: #165DFF;
}

.page-btn.disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

/* 工具类 */
.ml-1 { margin-left: 0.25rem; }
.mr-1 { margin-right: 0.25rem; }
</style>    